﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using PopupControl;
using SharpDiary.Core.Utility;
using SharpDiary.Core.Drawing;
using SharpDiary.Core.ORMapping;

namespace SharpDiary.Core.Controls
{
    public partial class EmotionButtonControl : UserControl
    {
        private EmotionEditor _emotionEditor = new EmotionEditor();
        private bool _emotionShown = false;

        public EmotionInfo Emotion
        {
            get { return _emotionEditor.Emotion; }
            set { _emotionEditor.Emotion = value; }
        }

        public string Caption
        {
            get { return lblText.Text; }
            set { lblText.Text = value; }
        }

        public EmotionButtonControl()
        {
            InitializeComponent();

            DoubleBuffered = true;
            ResizeRedraw = true;

            BackColor = Color.Transparent;
        }

        private void lblText_Click(object sender, EventArgs e)
        {
            if (!_emotionShown) { _emotionEditor.ShowEmotions(); _emotionShown = true; }

            Popup popup = new Popup(_emotionEditor);
            popup.Closed += new ToolStripDropDownClosedEventHandler(popup_Closed);
            popup.Show(lblText, new Point(0, 0), ToolStripDropDownDirection.AboveRight);
        }

        private void popup_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            
            if (_emotionEditor.Emotion == null || string.IsNullOrEmpty(_emotionEditor.Emotion.Name))
            {
                lblText.Image = null;
                lblText.Text = "心情";
                toolTip.SetToolTip(lblText, (_emotionEditor.Emotion != null ? _emotionEditor.Emotion.Name :
                    "点击此处设置心情。"));
            }
            else
            {
                Image img = Utils.BytesToImage(_emotionEditor.Emotion.Picture);

                lblText.Text = string.Empty;
                lblText.Image = PaintHelper.ScaleImage(img, new Size(16, 16)); ;
                toolTip.SetToolTip(lblText, (_emotionEditor.Emotion != null ? _emotionEditor.Emotion.Name :
                    "点击此处设置心情。"));
            }
        }


    }
}
